﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Aulac.Verb.Ems;
using Aulac.Global.Xml;
using Aulac.Rsc.Ems.Tags;
using Aulac.Rsc;
using Aulac.Global.Enums;
using Aulac.Client.Global.Interface;
using Aulac.Global.Common;
using System.Data;
using Aulac.Global.Data;

namespace Aulac.Client.Ems.WinForm.Stores
{
    partial class FormRecMdnStore
    {
        private bool SaveRecMdnStore(FormModes pFormMode, ref XmlDataDocument xmlData)
        {
            try
            {
                string sVerb = (pFormMode == FormModes.NewMode) ? AulacEmsVerb.MngStore.RecStore.AddVchRecStore : AulacEmsVerb.MngStore.RecStore.UpdateVchRecStore;
                XmlDataDocument xmlDoc = CreateXmlDocument(sVerb, AulacEmsVerb.MngStore.RecStore.Noun.RecvStore);
                XmlElement eHeader = XmlProcess.AddVchHeaderElm(ref xmlDoc);
                
                foreach (Aulac.Client.Global.Interface.IAulacValueDataCtrl ctr in GetListControlsOnForm())
                    ctr.GetValueToXml(ref eHeader);
                XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.TagStore, TagStores.InputStore);
                XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.VchName, mVchName);
                XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.VchTypeID, mVchType);
                XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.PartTypeID, cbPrvd.ComboObjectTyeps.ObjTypeID);
                XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.PartPrkID, cbPrvd.ObjPrkID);
                XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.PartName, cbPrvd.ObjName);
                XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.IsCheckQtyLost, mIsCheckInstock);


                if (pFormMode == FormModes.EditMode)
                    XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.VchRecInvPrkID, mVoucherID);
                
                XmlElement eLines = XmlProcess.AddVchLinesElm(ref xmlDoc);
                
                int iCountLine = grdLine.grdData.ReadOnly == true ? grdLine.grdData.Rows.Count : grdLine.grdData.Rows.Count - 1;
                for (int i = 1; i < iCountLine; i++)
                {
                    grdLine.grdData.SetValueCell(i, tagVch.Inv.StorePrkID, cbStore.StorePrkID);
                    double sPriceBuyInExc =(double) grdLine.grdData.GetValueDataCell(i, tagVch.Inv.PriceInExc);
                    grdLine.grdData.SetValueCell(i, tagVch.Inv.PriceBuyInExc, sPriceBuyInExc);
                }
                ((IAulacDataContainerCtrl)grdLine).GetDataToChildNodeXml(eLines);


                // Chuyen nhung gia tri o duoi ve dang khong co dinh dang dau ','
                XmlNodeList mNodeList = XmlProcess.GetListVchLineItem(xmlDoc);
                foreach (XmlElement Elmt in mNodeList)
                {
                    XmlElement eL = Elmt;
                    string sPriceBuyInExc = eL.Attributes[tagVch.Inv.PriceBuyInExc].Value.ToString().Replace(",", ".");
                    string sAmtLineInExc = eL.Attributes[tagVch.Inv.AmtLineInExc].Value.ToString().Replace(",", ".");
                    string sPrimeCostInExc = eL.Attributes[tagVch.Inv.PrimeCostInExc].Value.ToString().Replace(",", ".");
                    string sAmtTaxLineInExc = eL.Attributes[tagVch.Inv.AmtTaxLineInExc].Value.ToString().Replace(",", ".");
                    string sAmtTtlLineInExc = eL.Attributes[tagVch.Inv.AmtTtlLineInExc].Value.ToString().Replace(",", ".");
                    string sAmtPrimeCostLineInExc = eL.Attributes[tagVch.Inv.AmtPrimeCostLineInExc].Value.ToString().Replace(",", ".");

                    XmlProcess.SetAtrb(ref eL, tagVch.Inv.PriceBuyInExc, sPriceBuyInExc);
                    XmlProcess.SetAtrb(ref eL, tagVch.Inv.AmtLineInExc, sAmtLineInExc);
                    XmlProcess.SetAtrb(ref eL, tagVch.Inv.PrimeCostInExc, sPrimeCostInExc);
                    XmlProcess.SetAtrb(ref eL, tagVch.Inv.AmtTaxLineInExc, sAmtTaxLineInExc);
                    XmlProcess.SetAtrb(ref eL, tagVch.Inv.AmtTtlLineInExc, sAmtTtlLineInExc);
                    XmlProcess.SetAtrb(ref eL, tagVch.Inv.AmtPrimeCostLineInExc, sAmtPrimeCostLineInExc);
                }
                xmlData = DataRequest.ProcessRequest(xmlDoc);
                return true;
            }
            catch (Exception ex)
            {
                Aulac.Global.Exceptions.AulacException x = new Aulac.Global.Exceptions.AulacException(ex);
                if (ErrCodes.ErrorCode.Error0010.Equals(x.ErrorCode))
                {
                    XmlNodeList lst = XmlProcess.GetListVchLineItem(x.XmlEcexption);
                    foreach (XmlElement eLine in lst)
                    {
                        DataRow row = grdLine.AulacDataGrid.GetFirstDataRowByKey(tagVch.Inv.ItemPrkID, XmlProcess.GetValueAtrb(eLine, tagVch.Inv.ItemPrkID));
                        DataTableProcess.SetValueDataRow(row, tagVch.Inv.OverInstock, true);
                    }
                    return false;
                }
                else throw ex;

            }
        }
        private string GetNewVchNo(ref string pVchName)
        {
            XmlDataDocument xmlDoc = DataRequest.CreateXmlDocument(Aulac.Verb.Ems.AulacEmsVerb.MngStore.RecStore.GetNewVchNo, Aulac.Verb.Ems.AulacEmsVerb.MngStore.RecStore.Noun.RecvStore);
            XmlProcess.AddValueParamItem(xmlDoc, tagVch.Inv.VchTypeID, mVchType);
            XmlDataDocument outDoc = DataRequest.ProcessRequest(xmlDoc);
            pVchName = XmlProcess.GetValueVchHeader(outDoc, tagVch.Inv.VchName);
            return XmlProcess.GetValueVchHeader(outDoc, tagVch.Inv.VchNo);
        }
    }
}
